#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
namespace dict_ns
{
    const std::string defaultpath = "./Dict.txt";
    const std::string sep = ":";
    class Dict
    {
    private:
        bool Load()
        {
            std::cout<<"Load is running"<<std::endl;
            std::ifstream in(_dict_conf_filepath);
            if (!in.is_open())
            {
                perror("open file error!!");
                return false;
            }
            std::string line;
            std::cout<<"open success~"<<std::endl;
            while (std::getline(in, line))
            {
                std::cout<<line<<std::endl;
                if (line.empty())
                    continue;
                auto pos = line.find(sep);
                if (pos == std::string::npos)
                    continue;
                std::string word = line.substr(0, pos);
                if(word.empty()) continue;
                std::string han = line.substr(pos+sep.size());
                if(han.empty()) continue;

                _dict.insert(std::make_pair(word,han));
                std::cout<<"insert success: han: " <<han<<"English: "<<word <<std::endl;
            }

            in.close();
            std::cout<<"load success~"<<"filepath: "<<_dict_conf_filepath <<std::endl;
            return true;
        }

    public:
        Dict(const std::string &path = defaultpath) : _dict_conf_filepath(path)
        {
            Load();
        }
        ~Dict()
        {
        }
        std::string Translate(const std::string &word,bool &ok)
        {  
            ok = true;
            auto iter = _dict.find(word);
            if(iter == _dict.end())
            {
                ok = false;
                return "未找到";
                // std::cout<<""<<std::endl;
            }
            return iter->second;
        }

    private:
        std::unordered_map<std::string, std::string> _dict;
        std::string _dict_conf_filepath;
    };
}
